package com.ebocy.hammer.battle.fight.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Map.Entry;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.log4j.Logger;
import org.apache.mina.core.session.IoSession;

import com.ebocy.hammer.battle.BattleCache;
import com.ebocy.hammer.battle.fight.message.BattleResutReport;
import com.ebocy.hammer.battle.fight.message.S2CAfterWormUpMessage;
import com.ebocy.hammer.battle.fight.message.S2CAttackMessage;
import com.ebocy.hammer.battle.fight.message.S2CBrickMessage;
import com.ebocy.hammer.battle.fight.message.S2CBufferStopMessage;
import com.ebocy.hammer.battle.fight.message.S2CClimbLadderMessage;
import com.ebocy.hammer.battle.fight.message.S2CCombatContinueIPMessage;
import com.ebocy.hammer.battle.fight.message.S2CCombatContinueNotifyMessage;
import com.ebocy.hammer.battle.fight.message.S2CCombatEndMessage;
import com.ebocy.hammer.battle.fight.message.S2CDizzyMessage;
import com.ebocy.hammer.battle.fight.message.S2CDropItemMessage;
import com.ebocy.hammer.battle.fight.message.S2CFallMessage;
import com.ebocy.hammer.battle.fight.message.S2CFallingMessage;
import com.ebocy.hammer.battle.fight.message.S2CHomePageIPMessage;
import com.ebocy.hammer.battle.fight.message.S2CItemAttackMessage;
import com.ebocy.hammer.battle.fight.message.S2CItemFallMessage;
import com.ebocy.hammer.battle.fight.message.S2CMemberCoodinateMessage;
import com.ebocy.hammer.battle.fight.message.S2CMoveMessage;
import com.ebocy.hammer.battle.fight.message.S2CSafeLandMessage;
import com.ebocy.hammer.battle.fight.message.S2CSafeLandingMessage;
import com.ebocy.hammer.battle.fight.message.S2CSwitchNomalMessage;
import com.ebocy.hammer.battle.fight.message.S2CTouchItemMessage;
import com.ebocy.hammer.battle.fight.message.S2CUpdateFightStatisticMessage;
import com.ebocy.hammer.battle.fight.message.S2CUseItemMessage;
import com.ebocy.hammer.battle.fight.message.S2CWormUpAttackMessage;
import com.ebocy.hammer.battle.fight.message.S2CWormUpMessage;
import com.ebocy.hammer.core.GameCache;
import com.ebocy.hammer.core.constants.ErrorCode;
import com.ebocy.hammer.core.constants.GameConstants;
import com.ebocy.hammer.core.domain.Block;
import com.ebocy.hammer.core.domain.Buffer;
import com.ebocy.hammer.core.domain.GamePlayer;
import com.ebocy.hammer.core.domain.Hammer;
import com.ebocy.hammer.core.domain.Item;
import com.ebocy.hammer.core.domain.Room;
import com.ebocy.hammer.core.log.EbocyLogManager;
import com.ebocy.hammer.core.message.AbstractS2CMessage;
import com.ebocy.hammer.core.message.S2CErrorCode;
import com.ebocy.hammer.core.service.ItemService;
import com.ebocy.hammer.core.util.Config;
import com.ebocy.hammer.core.util.ServerAddress;

public class FightService {
	protected static Logger logger = EbocyLogManager.getSystemLogger();
	private static AtomicInteger atomicInteger4Name = new AtomicInteger();
	private static AtomicInteger atomicInteger4Room = new AtomicInteger();
	private static Random random = new Random();
	private static ItemService itemService = null;

	private FightService() {
		if (itemService == null) {
			itemService = ItemService.getInstance();
		}
	}

	public static FightService getInstance() {
		return SingletonClassInstance.instance;
	}

	/**
	 * @author Qingrong Hou 构造线程安全的单例模式 May 16, 2012
	 */
	private static class SingletonClassInstance {
		private static final FightService instance = new FightService();
	}

	private static String getRandomString(int length) {
		String str = "abcdefghigklmnopkrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ0123456789";
		Random random = new Random();
		StringBuffer sf = new StringBuffer();
		for (int i = 0; i < length; i++) {
			int number = random.nextInt(62);// 0~61
			sf.append(str.charAt(number));
		}
		return sf.toString();
	}

	public void enterRoom(String userId, GamePlayer gamePlayer, IoSession session) {
		/*
		 * 
		 * Map<Long, Room> rooms = battleCache.getOnlineRooms();
		 * IDao<GamePlayer> dao = DaoManager.getInstance()
		 * .getDao(GamePlayer.class); if (userId == null || userId.length() ==
		 * 0) { logger.error("用户信息接受错误！userId=" + userId); return; }
		 * 
		 * // 获取房间 Room room = null; for (Room m : rooms.values()) {// 如果有房间 if
		 * (m.size() < FightContants.PLAYERS_NUM_PER_ROOM) { room = m; break; }
		 * } if (room == null) {// 当前没有房间 room = new
		 * Room(atomicInteger4Room.getAndIncrement());
		 * battleCache.addRoom(room); }
		 * 
		 * if (gamePlayer == null) { String userName = "test" +
		 * atomicInteger4Name.getAndIncrement(); gamePlayer =
		 * dao.findOne("name", userName); if (gamePlayer == null) { gamePlayer =
		 * new GamePlayer(); gamePlayer.setName(userName);
		 * dao.insert(gamePlayer); } gamePlayer.setSession(session);
		 * battleCache.addGamePlayer(gamePlayer); }
		 * 
		 * // int index=new Random().nextInt(blocks.size()); // Block
		 * block=blocks.get(index); gamePlayer.setX((short) 2);
		 * gamePlayer.setY((short) 4); gamePlayer.setDir((byte) -1);
		 * 
		 * room.addGamePlayer(gamePlayer);
		 * 
		 * // 返回房间玩家坐标 AbstractS2CMessage message = new S2CEnterRoomMessage(
		 * room.getGamePlayers()); gamePlayer.sendMessage(message);
		 * 
		 * // 通知其他玩家 AbstractS2CMessage message1 = new
		 * S2CEnterRoomNotifyMessage(gamePlayer);
		 * gamePlayer.sendMessageToRoomOther(message1);
		 */
	}

	/**
	 * 移动
	 * 
	 * @param dir
	 */
	public void move(GamePlayer gamePlayer, short x, short y, byte dir) {
		gamePlayer.setX(x);
		gamePlayer.setY(y);
		gamePlayer.setDir(dir);
		S2CMoveMessage message = new S2CMoveMessage(String.valueOf(gamePlayer.getGamePlayerId()), x, y, dir);
		gamePlayer.sendMessageToRoomOther(message);
	}

	/**
	 * 攻击
	 */
	public void attack(GamePlayer gamePlayer, byte dir) {
		gamePlayer.setDir(dir);
		S2CAttackMessage message = new S2CAttackMessage(String.valueOf(gamePlayer.getGamePlayerId()), dir);
		gamePlayer.sendMessageToRoomOther(message);
		drop(gamePlayer);
	}

	/**
	 * 蓄力攻击
	 */
	public void wormUpAttack(GamePlayer gamePlayer, byte dir) {
		S2CWormUpAttackMessage message = new S2CWormUpAttackMessage(String.valueOf(gamePlayer.getGamePlayerId()), dir);
		gamePlayer.sendMessageToRoomOther(message);
	}

	/**
	 * 移除buffer
	 */
	public void bufferRemove(GamePlayer gamePlayer, int bufferId) {
		S2CBufferStopMessage message = new S2CBufferStopMessage(String.valueOf(gamePlayer.getGamePlayerId()), bufferId);
		gamePlayer.sendMessageToRoomOther(message);
	}

	/**
	 * 掉落计算
	 */
	public void drop(GamePlayer gamePlayer) {
		// 掉落计算
		int itemId = 0;
		int seed = random.nextInt(100);
		if (seed < 5) {
			Room room = GameCache.getRoom(gamePlayer.getRoomId());
			itemId = GameConstants.DROP_ITEM_RANGE[random.nextInt(GameConstants.DROP_ITEM_RANGE.length)];
			int uuid = room.getUUID();
			Hammer hammer = GameCache.hammerData.get(gamePlayer.getHammerId());
			int dir = gamePlayer.getDir();
			Integer[] block = new Integer[3];

			switch (dir) {
			case 1:/** 攻击方向为上，值为1 **/
				block[1] = (int) gamePlayer.getY() - 25 * 4;
				block[0] = (int) gamePlayer.getX();
				break;
			case 2:/** 攻击方向为下，值为2 **/
				block[1] = (int) gamePlayer.getY();
				block[0] = (int) gamePlayer.getX();
				break;
			case 3:/** 攻击方向为左，值为3 **/
				block[0] = (int) gamePlayer.getX() - hammer.getAttackDis() * 40;
				block[1] = (int) gamePlayer.getY();
				break;
			case 4:/** 攻击方向为右，值为4 **/
				block[0] = (int) gamePlayer.getX() + hammer.getAttackDis() * 40;
				block[1] = (int) gamePlayer.getY();
				break;
			}
			block[2] = itemId;
			room.mapBlock.put(uuid, block);
			room.saveRoome();
			AbstractS2CMessage message = new S2CDropItemMessage(gamePlayer.getGamePlayerId(), uuid, itemId, block[0], block[1]);
			gamePlayer.sendMessageToRoom(message);
		}
	}

	/**
	 * 蓄力
	 */
	public void wormUp(GamePlayer gamePlayer) {
		S2CWormUpMessage message = new S2CWormUpMessage(gamePlayer);
		gamePlayer.sendMessageToRoomOther(message);
	}

	/**
	 * 眩晕
	 */
	public void dizzy(GamePlayer gamePlayer, short x, short y) {
		gamePlayer.setX(x);
		gamePlayer.setY(y);
		S2CDizzyMessage message = new S2CDizzyMessage(String.valueOf(gamePlayer.getGamePlayerId()), x, y);
		gamePlayer.sendMessageToRoomOther(message);

		// 记录眩晕次数
		if (gamePlayer.killer != 0) {
			gamePlayer.countOfDizzy += 1;
			S2CUpdateFightStatisticMessage statisticMessage = new S2CUpdateFightStatisticMessage(String.valueOf(gamePlayer.getGamePlayerId()), S2CUpdateFightStatisticMessage.DIZZY, gamePlayer.countOfDizzy);
			gamePlayer.sendMessageToRoom(statisticMessage);
		}
	}

	/**
	 * 被道具攻击
	 */
	public void attackByItem(GamePlayer gamePlayer, short itemId) {
		S2CItemAttackMessage message = new S2CItemAttackMessage(String.valueOf(gamePlayer.getGamePlayerId()), itemId);
		gamePlayer.sendMessageToRoomOther(message);
	}

	/**
	 * 安全降落中
	 */
	public void safeLanding(GamePlayer gamePlayer, short x, short y) {
		gamePlayer.setX(x);
		gamePlayer.setY(y);
		S2CSafeLandingMessage message = new S2CSafeLandingMessage(String.valueOf(gamePlayer.getGamePlayerId()), x, y);
		gamePlayer.sendMessageToRoomOther(message);
	}

	/**
	 * 安全降落结束
	 */
	public void safeLand(GamePlayer gamePlayer, short x, short y) {
		gamePlayer.setX(x);
		gamePlayer.setY(y);
		S2CSafeLandMessage message = new S2CSafeLandMessage(String.valueOf(gamePlayer.getGamePlayerId()), x, y);
		gamePlayer.sendMessageToRoomOther(message);
	}

	/**
	 * 坠落开始
	 */
	public void fall(GamePlayer gamePlayer, short x, short y) {
		gamePlayer.setX(x);
		gamePlayer.setY(y);
		// gamePlayer.fallingPosition[0] = x;
		// gamePlayer.fallingPosition[1] = y;
		S2CFallMessage message = new S2CFallMessage(String.valueOf(gamePlayer.getGamePlayerId()), x, y);
		gamePlayer.sendMessageToRoomOther(message);
	}

	/**
	 * 坠落中
	 */
	public void falling(GamePlayer gamePlayer, short x, short y) {
		gamePlayer.setX(x);
		gamePlayer.setY(y);
		S2CFallingMessage message = new S2CFallingMessage(String.valueOf(gamePlayer.getGamePlayerId()), x, y);
		gamePlayer.sendMessageToRoomOther(message);
	}

	/**
	 * 爬楼梯
	 */
	public void climbLadder(GamePlayer gamePlayer, short x, short y) {
		gamePlayer.setX(x);
		gamePlayer.setY(y);
		S2CClimbLadderMessage message = new S2CClimbLadderMessage(String.valueOf(gamePlayer.getGamePlayerId()), x, y);
		gamePlayer.sendMessageToRoomOther(message);
	}

	/**
	 * 自动砖头持久
	 */
	public void brick(GamePlayer gamePlayer, short x, short y, short endurance) {
		S2CBrickMessage message = new S2CBrickMessage(String.valueOf(gamePlayer.getGamePlayerId()), x, y, endurance);
		gamePlayer.sendMessageToRoomOther(message);
	}

	/**
	 * 进入一般状态
	 */
	public void switchNomalStatus(short x, short y, GamePlayer gamePlayer) {
		gamePlayer.setX(x);
		gamePlayer.setY(y);
		// gamePlayer.fallingPosition[0] = 0;
		// gamePlayer.fallingPosition[1] = 0;
		gamePlayer.killer = 0;
		S2CSwitchNomalMessage message = new S2CSwitchNomalMessage(gamePlayer);
		gamePlayer.sendMessageToRoomOther(message);
	}

	/**
	 * 道具使用
	 * 
	 * @param gamePlayer
	 *            用户对象
	 * @param sign
	 * @param itemId
	 *            物品id
	 * @param x
	 *            坐标x
	 * @param y
	 *            坐标y
	 */
	public void useItem(GamePlayer gamePlayer, int itemId, short x, short y) {
		// 获取房间信息
		Room room = GameCache.getRoom(gamePlayer.getRoomId());
		boolean bool = itemService.checkItem(gamePlayer, itemId);
		AbstractS2CMessage message = null;
		if (bool) {
			// 查询物品详细
			Item item = itemService.findItemById(itemId);
			// 判断道具使用对象
			// 对自己 0,对敌人1,对所有人 2
			int isOppose = item.getIsOppose();
			if (isOppose == 0) {//
				// 获取玩家当前buff效果
				Map<Integer, Buffer> playerBuffers = gamePlayer.getBuffer();
				// 创建buffer
				Buffer buffer = new Buffer(itemId, item.getType(), System.currentTimeMillis(), item.getCoolDown(), item.getTime());
				// 当前buffer为空
				if (playerBuffers == null) {
					playerBuffers = new HashMap<Integer, Buffer>();
					// 玩家增加buffer效果
					playerBuffers.put(itemId, buffer);
				} else {
					playerBuffers.put(itemId, buffer);
				}
				// 设置buffer
				gamePlayer.setBuffer(playerBuffers);
				int uuid = room.getUUID();
				message = new S2CUseItemMessage(gamePlayer.getGamePlayerId(), uuid, itemId, x, y);
			} else {
				// 获取场景道具缓存
				Integer[] block = new Integer[3];
				block[0] = (int) x;
				block[1] = (int) y;
				block[2] = itemId;
				int uuid = room.getUUID();
				room.mapBlock.put(uuid, block);
				room.saveRoome();
				message = new S2CUseItemMessage(gamePlayer.getGamePlayerId(), uuid, itemId, x, y);
			}
		} else {
			// 错误提示信息,物品不存在或未装备
			message = new S2CErrorCode(ErrorCode.ITEM_NOT_ON);
		}
		gamePlayer.sendMessageToRoom(message);
	}

	/**
	 * 道具触发
	 * 
	 * @param gamePlayer
	 *            用户对象
	 * @param uuid
	 *            物品唯一id
	 * @param x
	 *            坐标x
	 * @param y
	 *            坐标y
	 */
	public void touchItem(GamePlayer gamePlayer, int uuid, int itemId, short x, short y) {
		// 获取房间信息
		Room room = GameCache.getRoom(gamePlayer.getRoomId());
		boolean bool = false;
		if (room.mapBlock != null) {
			bool = room.mapBlock.containsKey(uuid);
		}
		AbstractS2CMessage message = null;
		if (bool) {
			// 获取玩家当前buff效果
			Map<Integer, Buffer> playerBuffers = gamePlayer.getBuffer();
			// 查询物品详细
			Item item = itemService.findItemById(itemId);
			// 创建buffer
			Buffer buffer = new Buffer(itemId, item.getType(), System.currentTimeMillis(), item.getCoolDown(), item.getTime());
			// 当前buffer为空
			if (playerBuffers == null) {
				playerBuffers = new HashMap<Integer, Buffer>();
				// 玩家增加buffer效果
				playerBuffers.put(itemId, buffer);
			} else {
				playerBuffers.put(itemId, buffer);
			}
			room.mapBlock.remove(uuid);
			room.saveRoome();
			// 设置buffer
			gamePlayer.setBuffer(playerBuffers);
			message = new S2CTouchItemMessage(gamePlayer.getGamePlayerId(), uuid, itemId, x, y);
		} else {
			// 错误提示信息,物品不存在或未装备
			message = new S2CErrorCode(ErrorCode.ITEM_NOT_ON);
		}
		gamePlayer.sendMessageToRoom(message);
	}

	/**
	 * 战斗准备 1.重新入队 2.满员后分配玩家坐标，同时分组
	 */
	public synchronized  void prepare4V4Combat(GamePlayer gamePlayer, int roomId, String mapName) {
		gamePlayer.countOfKill = 0;
		gamePlayer.countOfDizzy = 0;
		gamePlayer.setRoomId(roomId);
		Room room = GameCache.getRoom(roomId);
		room.state = 1;
		room.mapName = mapName;
		room.addBattle(gamePlayer.getGamePlayerId(), gamePlayer); // 玩家入队
	}

	public synchronized void battleStart(GamePlayer gamePlayer) {
		int roomId = gamePlayer.getRoomId();
		Room room = GameCache.getRoom(roomId);
		room.setResInit(gamePlayer.getGamePlayerId());
		// 房间人员已满足
		if (room.inits.contains(gamePlayer.getGamePlayerId())) {
			if (room.inits.size() == room.worlds.size() - room.losts.size()) {
				randomMemberCoordinate(room.mapName, room); // 随机分配坐标
				grouping4V4Combat(room); // 分组
				// 将坐标及分组信息通知房间内玩家
				for (Long member : room.battles) {
					GamePlayer gpl = GameCache.getInstance().getCache(member);
					AbstractS2CMessage coordinateMessage = new S2CMemberCoodinateMessage(room.mapName, room.battles);
					gpl.sendMessage(coordinateMessage);
				}
				// 战斗开始,改变房间状态
				room.saveRoome(2);
				// 记录战斗时间
				BattleCache.recordCombatTimestamp(roomId);
			}
		}
	}

	/**
	 * 战报处理、发放奖励
	 */
	public void calculateBattleResult(Room room) {
		// 计算奖励
		double seed = Math.random();
		short winTeamRewardExp = (short) ((short) (seed * (GameConstants.WIN_TEAM_REWARD_EXP[1] - GameConstants.WIN_TEAM_REWARD_EXP[0])) + GameConstants.WIN_TEAM_REWARD_EXP[0]);
		short winTeamRewardCoin = (short) ((short) (seed * (GameConstants.WIN_TEAM_REWARD_COIN[1] - GameConstants.WIN_TEAM_REWARD_COIN[0])) + GameConstants.WIN_TEAM_REWARD_COIN[0]);
		short failTeamRewardExp = (short) ((short) (seed * (GameConstants.FAIL_TEAM_REWARD_EXP[1] - GameConstants.FAIL_TEAM_REWARD_EXP[0])) + GameConstants.FAIL_TEAM_REWARD_EXP[0]);
		short failTeamRewardCoin = (short) ((short) (seed * (GameConstants.FAIL_TEAM_REWARD_COIN[1] - GameConstants.FAIL_TEAM_REWARD_COIN[0])) + GameConstants.FAIL_TEAM_REWARD_COIN[0]);

		// 判断哪一队胜利
		short teamAKillCount = 0, teamBKillCount = 0, winTeamId = 0;
		for (Long member : room.battles) {
			GamePlayer gpl = GameCache.getInstance().getCache(member);
			if (gpl.getTeamId() == GameConstants.DEFAULT_TEAM_ID)
				teamAKillCount += gpl.countOfKill;
			else
				teamBKillCount += gpl.countOfKill;
		}
		if (teamAKillCount > teamBKillCount)
			winTeamId = GameConstants.DEFAULT_TEAM_ID;

		// 发放奖励并组合返回消息
		List<BattleResutReport> battleResutReports = new ArrayList<BattleResutReport>();
		for (Long member : room.battles) {
			BattleResutReport report = null;
			GamePlayer gpl = GameCache.getInstance().getCache(member);
			if (gpl.getTeamId() == winTeamId) {
				report = new BattleResutReport(String.valueOf(gpl.getGamePlayerId()), gpl.countOfKill, gpl.countOfDizzy, winTeamRewardExp, winTeamRewardCoin, (byte) 1);
			} else {
				report = new BattleResutReport(String.valueOf(gpl.getGamePlayerId()), gpl.countOfKill, gpl.countOfDizzy, failTeamRewardExp, failTeamRewardCoin, (byte) 0);
			}
			battleResutReports.add(report);
		}

		// 通知房间内所有玩家
		AbstractS2CMessage reportMessage = new S2CCombatEndMessage(battleResutReports);
		for (Long member : room.battles) {
			GamePlayer gpl = GameCache.getInstance().getCache(member);
			gpl.sendMessage(reportMessage);
		}
	}

	/**
	 * 指定地图情况下，为组内成员随机坐标
	 */
	private synchronized void randomMemberCoordinate(String mapName, Room room) {
		String record = "";
		int size = room.battles.size();
		short[][] xy = new short[size][2];
		int count = 0;
		logger.info("房间ID为：" + room.roomId + "的玩家进入的地图名" + mapName);
		// if (GameCache.blockMap.size() > 0) {
		// for (Entry<String, List<Block>> entry :
		// GameCache.blockMap.entrySet()) {
		// List<Block> blockList = entry.getValue();
		// logger.info("缓存地图中的地板格数：" + blockList.size());
		// }
		// } else {
		// logger.info(mapName + "这个名字的地图在缓存中不存在");
		// }
		while (true) {
			List<Block> blockList = GameCache.blockMap.get(mapName);
			int randomBolockIndex = random.nextInt(blockList.size());
			String tempRecord = "" + randomBolockIndex + "_";
			if (record.indexOf(tempRecord) >= 0) // 已经分配的坐标跳过
				continue;
			record += tempRecord;
			Block block = blockList.get(randomBolockIndex);
			if (block.getRow() > 1 && block.getCol() > 1) {
				// short x = (short) (block.getRow() * 25); // 将行、列转为x、y
				// short y = (short) (block.getCol() * 40);
				short x = (short) (block.getRow()); // 将行、列转为x、y
				short y = (short) (block.getCol());
				xy[count][0] = x;
				xy[count][1] = y;
				count++;
				if (count == size) // 已随机出8组，循环退出
					break;
			}
		}

		count = 0;
		for (Long member : room.battles) {
			GamePlayer gpl = GameCache.getInstance().getCache(member);
			gpl.setX(xy[count][0]);
			gpl.setY(xy[count][1]);
			gpl.setTeamId((byte) 0);
			count++;
		}
	}

	/**
	 * 4对4战斗模式，根据等级分组；等级排序后，1/3/6/8为一组，2/4/5/7为一组；默认第一队teamId为0，另一队teamId为1
	 */
	private void grouping4V4Combat(Room room) {
		ArrayList<Long> orderedList = new ArrayList<Long>(room.battles);
		for (int i = 0; i < orderedList.size(); i++) {
			GamePlayer gpl = GameCache.getInstance().getCache(orderedList.get(i));
			if (i % 2 == 0) {
				gpl.setTeamId(GameConstants.DEFAULT_TEAM_ID);
			} else {
				gpl.setTeamId((byte) 0);
			}
		}

		// Collections.sort(orderedList,
		// new Comparator<GamePlayer>() {
		// public int compare(GamePlayer o1, GamePlayer o2) {
		// return ((Comparable<Short>) o1.getLevel()).compareTo( o2.getLevel());
		// }
		// });
		// orderedList.get(1).setTeamId((byte)1);
		// orderedList.get(3).setTeamId((byte)1);
		// orderedList.get(4).setTeamId((byte)1);
		// orderedList.get(6).setTeamId((byte)1);
	}

	/**
	 * 蓄力后
	 */
	public void afterWormUpAttack(GamePlayer gamePlayer) {
		S2CAfterWormUpMessage message = new S2CAfterWormUpMessage(String.valueOf(gamePlayer.getGamePlayerId()));
		gamePlayer.sendMessageToRoomOther(message);
	}

	/**
	 * 将某地块砸碎
	 */
	public void breakBlock(GamePlayer gamePlayer, byte row, byte column, long[] deadPlayerIdArray) {
		// System.err.println("gameplayer x/y " + gamePlayer.getX() + ", " +
		// gamePlayer.getY() + " row " + row + " column " + column);
		// 计算地块上有几个玩家
		byte numberOfPlayerOnBlock = 0;
		Room room = GameCache.getRoom(gamePlayer.getRoomId());
		for (Long member : room.battles) {
			GamePlayer gpl = GameCache.getInstance().getCache(member);

			// if (gpl.killer != 0)
			// continue;

			// 判断被击杀玩家是否在房间
			boolean memberIsDead = false;
			for (long deadPlayerId : deadPlayerIdArray)
				if (gpl.getGamePlayerId() == deadPlayerId) {
					memberIsDead = true;
					break;
				}
			if (memberIsDead) { // 玩家被击杀，添加标记
				gpl.killer = gamePlayer.getGamePlayerId();
				numberOfPlayerOnBlock++;
			}

			// byte tempColumn = (byte) (member.[0] / 40);
			// System.err.println("x " + member.fallingPosition[0] +
			// " tempColumn " +tempColumn);
			// byte tempRow = (byte) (member.fallingPosition[1] / 25);
			// System.err.println("y " + member.fallingPosition[1] + " tempRow "
			// +tempRow);
			// if (row == tempRow && column == tempColumn) {
			// member.killer = gamePlayer.getGamePlayerId();
			// numberOfPlayerOnBlock++;
			// }
		}

		// 如果砸中地块上有玩家，更新并通知玩家击杀数
		if (numberOfPlayerOnBlock > 0) {
			gamePlayer.countOfKill += numberOfPlayerOnBlock;
			AbstractS2CMessage message = new S2CUpdateFightStatisticMessage(String.valueOf(gamePlayer.getGamePlayerId()), S2CUpdateFightStatisticMessage.KILL, gamePlayer.countOfKill);
			gamePlayer.sendMessageToRoom(message);
		}
	}

	/**
	 * 继续排队
	 */
	public void combatContinue(GamePlayer gamePlayer) {
		S2CCombatContinueNotifyMessage notifyMessage = new S2CCombatContinueNotifyMessage(gamePlayer.getGamePlayerId());
		gamePlayer.sendMessageToRoom(notifyMessage);
		S2CCombatContinueIPMessage ipMessage = new S2CCombatContinueIPMessage(Config.worldServer.getIp(), Config.worldServer.getPort());
		gamePlayer.sendMessage(ipMessage);
	}

	/**
	 * 返回主页
	 */
	public void combatLeave(GamePlayer gamePlayer) {
		// S2CCombatLeaveNotifyMessage notifyMessage = new
		// S2CCombatLeaveNotifyMessage(gamePlayer.getGamePlayerId());
		// gamePlayer.sendMessageToRoom(notifyMessage);
		for (ServerAddress address : Config.gameServerMap.values()) {
			if (address != null) {
				S2CHomePageIPMessage ipMessage = new S2CHomePageIPMessage(address.getIp(), address.getPort());
				gamePlayer.sendMessage(ipMessage);
				break;
			}
		}
	}

	/**
	 * 道具坠落过程
	 */
	public void itemFall(GamePlayer gamePlayer, String uuid, short x, short y) {
		S2CItemFallMessage message = new S2CItemFallMessage(String.valueOf(gamePlayer.getGamePlayerId()), uuid, x, y);
		gamePlayer.sendMessageToRoomOther(message);
	}

	// /**
	// * 退出游戏
	// */
	// public void exitGame(GamePlayer gamePlayer) {
	// if (gamePlayer != null) {
	// // 从缓存中去掉玩家记录
	// GameCache.getInstance().removeGamePlayer(gamePlayer);
	//
	// Room room = GameCache.room4V4Map.get(gamePlayer.getRoomId());
	// if (room != null) {
	// // 通知其他玩家
	// AbstractS2CMessage message = new
	// S2CExitGameMessage(gamePlayer.getGamePlayerId());
	// gamePlayer.sendMessageToRoomOther(message);
	//
	// // 从房间中退出
	// room.members.remove(gamePlayer.getGamePlayerId());
	// }
	// }
	// }

}
